<template>
  <content-card title="角色信息" show-reload @reload="getList">
    <template slot="right">
      <el-button icon="el-icon-plus" type="text" @click="handleAdd">添加授权</el-button>
      <el-button :disabled="multiple" icon="el-icon-circle-close" type="text" @click="handleDelete">取消授权</el-button>
    </template>
    <div>
      <el-form ref="queryForm" :inline="true" :model="queryParams" class="search-form" label-suffix="：">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="queryParams.roleName" clearable placeholder="请输入角色名称"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="角色类型" prop="roleType">
          <el-select v-model="queryParams.roleType" placeholder="请选择角色类型">
            <el-option v-for="item in localDict.role_type" :key="item.value" :label="item.label"
                       :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" type="info" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="dataList" :loading="loading" row-key="id" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column label="角色名称" min-width="160" prop="role.name"/>
        <el-table-column label="角色类型" min-width="160" prop="role.type">
          <template slot-scope="scope">
            <dict-tag :options="localDict.role_type" :value="scope.row.role.type" show-label/>
          </template>
        </el-table-column>
        <el-table-column label="数据权限" min-width="160" prop="scope">
          <template slot-scope="scope">
            <dict-tag :options="localDict.role_scope" :value="scope.row.role.scope" show-label/>
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="120" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="localDict.role_status" :value="scope.row.role.status" />
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="240">
          <template slot-scope="scope">
            <el-button icon="el-icon-tickets" size="mini" type="text" @click="handleDetail(scope.row)">详情</el-button>
            <el-button icon="el-icon-delete" type="text" @click="handleDelete(scope.row)">取消授权</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageParam.total > 0" :total="pageParam.total" :limit.sync="pageParam.pageSize"
                  :page.sync="pageParam.pageNum" :page-sizes="pageParam.options" @pagination="getList"/>
    </div>
  </content-card>
</template>

<script>
import {getStaticData} from "../../../../api/common";
import {remove, list, save} from "../../../../api/base";

export default {
  name: "RoleCard",
  components: {},
  props: {
    userId: String,
  },
  data() {
    return {
      ids: [],
      single: true,
      multiple: true,
      loading: false,
      baseUrl: '/system/user/role/',
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      queryParams: {},
      localDict: {
        role_type: [],
        role_scope: [],
        role_status: [],
      },
      dialog: {
        // 是否显示弹出层
        open: false,
        type: "",
        title: "",
      },
      formdata: {},
      formrules: {
        authType: {required: true, message: '请选择授权类型', trigger: 'blur'},
      },
    }
  },
  created() {
    this.getLocalDict()
    this.getList()
  },
  methods: {
    getLocalDict() {
      // getStaticData('system-auth-type').then(res => {
      //   this.localDict.auth_type = res.data
      // })
      getStaticData('system-role-type').then(res => {
        this.localDict.role_type = res.data
      })
      getStaticData('system-role-scope').then(res => {
        this.localDict.role_scope = res.data
      })
      getStaticData('system-status').then(res => {
        this.localDict.role_status = res.data
      })
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        userId: this.userId,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(res => {
          this.dataList = res.rows;
          this.pageParam.total = res.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.dialog.title = "添加授权"
      this.dialog.open = true
      this.dialog.type = 'add'
      this.formdata = {userId: this.userId, roleIds: []}
    },
    handleEdit(row) {
      this.dialog.open = true
      this.dialog.type = 'edit'
      this.dialog.title = "修改授权"
      this.formdata = {...row}
    },
    handleDetail(row) {
      this.$router.push({
        name: 'SysRoleDetail',
        path: `/system/role/detail/${row.id}`,
        params: {
          id: row.id
        }
      })
    },
    handleDelete(row) {
      const ids = [row.id] || this.ids
      this.$modal.confirm('是否确认取消选中的角色授权？').then(() => {
        remove(this.baseUrl, ids, true).then(() => {
          this.getList();
          this.$modal.msgSuccess("取消授权成功");
          this.single = true;
          this.multiple = true;
        }).catch(() => {
        })
      });
    },
    handleSubmit() {
      save(this.baseUrl, this.formdata).then(res => {
        console.log(res, '保存res=========>')
        this.$modal.msgSuccess('添加授权成功')
        this.$refs.roleCard.selectedRows = []
        this.$refs.roleCard.selectedKeys = []
        this.dialog.open = false
        this.getList()
      })
    },
    handleCancel() {
      this.dialog.open = false
      this.editable = false
    },
  }
}
</script>

<style scoped>

</style>
